package chakankonggui;

import until.DBUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AvailableLockersPanel extends JPanel {
    private JTable lockerTable;
    private DefaultTableModel tableModel;

    public AvailableLockersPanel() {
        setLayout(new BorderLayout(10, 10));
        setBackground(Color.WHITE);
        setBorder(BorderFactory.createEmptyBorder(20, 30, 20, 30));

        // 标题
        JLabel title = new JLabel("可用空柜列表", SwingConstants.CENTER);
        title.setFont(new Font("微软雅黑", Font.BOLD, 24));
        title.setForeground(new Color(40, 75, 99));
        title.setBorder(BorderFactory.createEmptyBorder(10, 0, 20, 0));
        add(title, BorderLayout.NORTH);

        // 表格模型
        String[] columnNames = {"柜体编号", "门编号", "柜子尺寸"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            // 禁止编辑单元格
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        lockerTable = new JTable(tableModel);
        lockerTable.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        lockerTable.setRowHeight(28);
        lockerTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 16));
        lockerTable.setFillsViewportHeight(true);
        lockerTable.setGridColor(new Color(220, 220, 220));
        lockerTable.setSelectionBackground(new Color(220, 235, 252));
        lockerTable.setBackground(new Color(250, 250, 255));

        JScrollPane scrollPane = new JScrollPane(lockerTable);
        scrollPane.setBorder(BorderFactory.createLineBorder(new Color(180, 180, 180)));
        add(scrollPane, BorderLayout.CENTER);

        // 刷新按钮
        JButton refreshBtn = new JButton("刷新空柜列表");
        refreshBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        refreshBtn.setFocusPainted(false);
        refreshBtn.setBackground(new Color(100, 149, 237));
        refreshBtn.setForeground(Color.WHITE);
        refreshBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        refreshBtn.setBorder(BorderFactory.createEmptyBorder(8, 20, 8, 20));
        refreshBtn.addActionListener(e -> loadAvailableLockers());

        JPanel btnPanel = new JPanel();
        btnPanel.setBackground(Color.WHITE);
        btnPanel.add(refreshBtn);
        add(btnPanel, BorderLayout.SOUTH);

        // 初始加载
        loadAvailableLockers();
    }

    private void loadAvailableLockers() {
        tableModel.setRowCount(0); // 清空表格
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "SELECT cabinet_number, door_number, size FROM lockers WHERE status = '空闲'";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();

            boolean found = false;
            while (rs.next()) {
                String cabinet = rs.getString("cabinet_number");
                int door = rs.getInt("door_number");
                String size = rs.getString("size");
                tableModel.addRow(new Object[]{cabinet, door, size});
                found = true;
            }

            if (!found) {
                JOptionPane.showMessageDialog(this, "当前没有空闲柜子。", "提示", JOptionPane.INFORMATION_MESSAGE);
            }

            rs.close();
            ps.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "加载失败：" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }
}
